﻿using BasicData.API.Read.Application.Commands;
using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infrastructure.interfaces;
using MD5Hash;
using MediatR;

namespace BasicData.API.Read.Application.CommandHandlers
{
    public class LoginCommandHandler : IRequestHandler<LoginCommand, APIResult<AccountModel>>
    {
        private readonly IBaseRepository<AccountModel> baseRepository;

        public LoginCommandHandler(IBaseRepository<AccountModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public async Task<APIResult<AccountModel>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            var list=baseRepository.GetAll().Where(x=>x.AccountName==request.AccountName).FirstOrDefault();
            if (list==null)
            {
                return APIResult<AccountModel>.Fail("账号不存在");
            }
            var pwd = request.AccountPassword.GetMD5();
            if(list.AccountPassword!=pwd)
            {
                return APIResult<AccountModel>.Fail("密码不正确");
            }
            return APIResult<AccountModel>.Success("登录成功", list);
        }
    }
}
